<?php
namespace addon\cms\logic;
use app\common\logic\LogicBase;
use \tpfcore\util\Tree;
use tpfcore\Core;
use tpfcore\helpers\StringHelper;
use think\Db;
use \think\Config;

class PostsImages extends LogicBase
{
	public function getPostsImages($where=[]){
		return self::getList($where);
	}
	public function savePostsImages($data){
		/* 验证文章 */
		$validate_posts=Core::addonValidate("Posts");
		$validate_result = $validate_posts->scene('add_posts_images')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $validate_posts->getError(), null];
        }
        
        $last_id=Core::loadAddonModel("Posts")->saveObject($data);

        /* 处理老图片 */
        if(!empty($data['old_desc'])){
        	$old_data_images =[];
        	foreach ($data['old_desc'] as $key => $value) {
        		$old_data_images[]=['id'=>$key,"desc"=>$value];
        	}
        	Core::loadAddonModel("PostsImages")->saveAll($old_data_images);
        	unset($data['old_desc']);
        }

        if($last_id){
        	/* 处理商品图片 */
	        if(!empty($data['url'])){

	        	$data_images=[];

	        	foreach ($data['url'] as $key => $value) {

	        		if(empty($value)) continue;

	        		/* 处理商品缩略图 */
					$thumb=$this->explode_thumb($value,120,120);

	        		$data_images[]=["posts_id"=>isset($data['id'])?$data['id']:$last_id,"url"=>$value,"desc"=>$data['desc'][$key],"thumb"=>$thumb];
	        	}
	        	Core::loadAddonModel($this->name)->saveAll($data_images);
	        }
	        return [RESULT_SUCCESS, '操作成功', url("cms/PostsImages/index")];

        }else{
        	return [RESULT_ERROR, '操作失败', null];
        }
	}

	public function del_imgae($data){
		// 删除真实图片
		if(isset($data['id'])){
			$posts_images = self::getOneObject($data,"url,thumb");

			if($posts_images){
				if(!empty($posts_images['url']) && is_file(ROOT_PATH.$posts_images['url'])){
					@unlink(ROOT_PATH.$posts_images['url']);
				}
				if(!empty($posts_images['thumb']) && is_file(ROOT_PATH.$posts_images['thumb'])){
					@unlink(ROOT_PATH.$posts_images['thumb']);
				}

				//删除数据
				$result = self::deleteObject($data,true);

				if($result){
					return [0,"操作成功",url('cms/PostsImages/index')];
				}
				return [40045,"操作失败"]; 
			}
			return [40045,"你删除的数据不存在"]; 
		}
		// 删除上传图片
		if(isset($data['url']) && is_file(ROOT_PATH.$data['url'])){
			@unlink(ROOT_PATH.$data['url']);
			return [0,"操作成功",url('cms/PostsImages/index')];
		}
		return [40045,"操作失败"];
	}

	public function delPostsImages($data){
		$listPostsImages = $this->getPostsImages(["where"=>["posts_id"=>$data['id']]]);

		if($listPostsImages){
			//这里可把真实图片删除
			foreach ($listPostsImages as $key => $value) {
				@unlink(ROOT_PATH.$value['thumb']);
				@unlink(ROOT_PATH.$value['url']);
			}
			if(!self::deleteObject(["posts_id"=>$data['id']],true)){
				return [RESULT_ERROR, '删除失败', url("cms/PostsImages/index")];
			}
		}

		if(Core::loadAddonModel("Posts")->deleteObject(['id'=>$data['id']],true)){
			return [RESULT_SUCCESS, '删除成功', url("cms/PostsImages/index")];	
		}else{
			return [RESULT_ERROR, '删除失败', url("cms/PostsImages/index")];
		}
	}
	
	private function explode_thumb($img,$width="200",$height="200"){

		if(empty($img)) return "";

		$arr = explode(".",$img);

    	$file_name = basename($img,".".end($arr))."_thumb.".end($arr);

    	$image = \think\Image::open('.'.$img);

    	$dir_name='./data/uploads/thumbs/'.date('Ym',time());

		if(!file_exists($dir_name)){

			mkdir($dir_name,0777,true);

		}

		$image->thumb($width, $height)->save($dir_name."/".$file_name);

		return '/data/uploads/thumbs/'.date('Ym',time()).'/'.$file_name;

	}
	
}